package com.first.group.authserver.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.first.group.authserver.entity.UserEntity;
import com.first.group.common.utils.PageUtils;
import com.first.group.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.first.group.authserver.entity.AddressEntity;
import com.first.group.authserver.service.AddressService;


/**
 * @author zy
 * @email 2067065884@qq.com
 * @date 2021-10-21 18:45:51
 */
@Slf4j
@RestController
@RequestMapping("authserver/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @RequestMapping("/treeById")
    public List<AddressEntity> treeById(@RequestParam("addressId") String addressId) {
        List<AddressEntity> byId = new ArrayList<>();
        String[] split = addressId.split(",");

        if (split[0] != null) {
            byId = addressService.list(new QueryWrapper<AddressEntity>().eq("address_id", new Long(split[0])));
            if (split[1] != null) {
                List<AddressEntity> address_parent_id = addressService.list(new QueryWrapper<AddressEntity>().eq("address_id", new Long(split[1])));
                for (AddressEntity addressEntity : byId) {
                    addressEntity.setChildren(address_parent_id);
                    if (split[2] != null) {
                        List<AddressEntity> address_parent_id2 = addressService.list(new QueryWrapper<AddressEntity>().eq("address_id", new Long(split[2])));
                        for (AddressEntity entity : address_parent_id) {
                            entity.setChildren(address_parent_id2);
                        }
                    }
                }
            }
        }

        return byId;
    }

    /**
     * 地址树
     */

    @RequestMapping("/treeList")
    public List<AddressEntity> treeList() {
        return addressService.treeList();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = addressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{addressId}")
    public R info(@PathVariable("addressId") Long addressId) {
        AddressEntity address = addressService.getById(addressId);

        return R.ok().put("address", address);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AddressEntity address) {
        addressService.save(address);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AddressEntity address) {
        addressService.updateById(address);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] addressIds) {
        addressService.removeByIds(Arrays.asList(addressIds));

        return R.ok();
    }

}
